package conterlles

import (
	"day12_10_homework/models/redis"
	"github.com/gin-gonic/gin"
	"math/rand/v2"
	"net/http"
	"time"
)

func SendSms(c *gin.Context) {
	tel := c.PostForm("tel")
	source := c.PostForm("source")
	num := "sendSms_" + source + tel
	loc := "sendSms_loc_" + source + tel
	get := redis.Rdb.Get(redis.Ctx, loc)
	if get.Val() != "" {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "请稍后再试",
			Data:    nil,
		})
		return
	}
	i := rand.IntN(9000) + 1000
	/*resp, err := pkg.SendSms(tel, strconv.Itoa(i))
	if err != nil {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "短信发送失败",
			Data:    nil,
		})
		return
	}
	if *resp.Body.Code != "OK" {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: *resp.Body.Message,
			Data:    nil,
		})
		return
	}*/
	redis.Rdb.Set(redis.Ctx, num, i, time.Minute*2)
	redis.Rdb.Set(redis.Ctx, loc, "短信锁", time.Minute*5)
	c.JSON(http.StatusOK, JsonRes{
		Code:    200,
		Message: "短信发送成功",
		Data:    nil,
	})
}
